/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonsbeans;


import entities.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import persistence.*;


/**
 *
 * @author Felipe Duarte
 * Esta Clase es definida para parametrizar las cargas de información de la bases de datos, con el objetivo
 * de cargarlas una sola vez en la session, permitiendo que la transaccionabilidad sea menor
 */
@ManagedBean
@ApplicationScoped
public class ApplicationBean {

    private final Date fechaActual = Calendar.getInstance().getTime();
    private final String[] forbiddenChars = {".", "!", "@", "#", "$", "%", "&", "*", "(", ")", "=", ",", ";", ":", "/", "?", "{", "}",
        "[", "]", "+", "-", "_"};
    private final String[] numbers = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    private final String[]letras={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    
    private String message;
    private DatoComplementarioJpaController datoComplementarioJpaController;
    private PrefijoJpaController prefijoJpaController = null;
    private TipoViaJpaController tipoViaJpaController = null;
    private List<DatoComplementario> datoComplementarioList = null;
    private List<Prefijo> prefijoList;
    private List<TipoVia> tipoViaList;
    private List<Modulo> moduloList;
    private List<SelectItem> datoComplementarioListItems;
    private List<SelectItem> prefijoListItems;
    private List<SelectItem> tipoViaListItems;
   
    private Object[] controllers = {datoComplementarioJpaController, prefijoJpaController, tipoViaJpaController};

    /** Creates a new instance of ApplicationBean */
    public ApplicationBean() {
    }

    //Constructor
    @PostConstruct
    public void init() {
        if (!areControllersInitilized(controllers)) {
            initControllers();
        }
        initLists();
        initItems();
    }

    //<editor-fold defaultstate="collapsed" desc="Funcionalidades">
    
    public void initLists() {
        datoComplementarioList = datoComplementarioJpaController.findDatoComplementarioEntities();
        prefijoList = prefijoJpaController.findPrefijoEntities();
        tipoViaList = tipoViaJpaController.findTipoViaEntities();
        
    }


    public void initItems() {
        datoComplementarioListItems = new ArrayList<SelectItem>();
        prefijoListItems = new ArrayList<SelectItem>();
        tipoViaListItems = new ArrayList<SelectItem>();
        for (int i = 0; i < datoComplementarioList.size(); i++) {
            datoComplementarioListItems.add(new SelectItem(datoComplementarioList.get(i).getNombreDatoComplementario(),
                    datoComplementarioList.get(i).getNombreDatoComplementario()));
        }
        for (int i = 0; i < prefijoList.size(); i++) {
            prefijoListItems.add(new SelectItem(prefijoList.get(i).getIdPrefijo(), prefijoList.get(i).getNombrePrefijo()));
        }
        for (int i = 0; i < tipoViaList.size(); i++) {
            tipoViaListItems.add(new SelectItem(tipoViaList.get(i).getNombreTipoVia(), tipoViaList.get(i).getNombreTipoVia()));
        }
    }
    
    public void showErrorMessage(String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
    
    public void showSuccessMessage(String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Operación exitosa", detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
    
     /**
     * Muestra mensajes para notificar advertencias
     * @param detail Detalle de acuerdo al módulo desde el cual se hace la transacción
     */
    public void showWarnMessage(String detail) {
        Logger.getLogger("Error en  bean").log(Level.WARNING, detail, detail);
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Advertencia", detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
  //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="getter's y setter's">
    public Date getFechaActual() {
        return Calendar.getInstance().getTime();
    }

    public String[] getForbiddenChars() {
        return forbiddenChars;
    }

   

    public String[] getLetras() {
        return letras;
    }

   

    public String[] getNumbers() {
        return numbers;
    }

   
    

    public List<Modulo> getModuloList() {
        return moduloList;
    }

    public void setModuloList(List<Modulo> moduloList) {
        this.moduloList = moduloList;
    }

    
    public List<SelectItem> getDatoComplementarioListItems() {
        return datoComplementarioListItems;
    }

    public void setDatoComplementarioListItems(List<SelectItem> datoComplementarioListItems) {
        this.datoComplementarioListItems = datoComplementarioListItems;
    }

    public List<SelectItem> getPrefijoListItems() {
        return prefijoListItems;
    }

    public void setPrefijoListItems(List<SelectItem> prefijoListItems) {
        this.prefijoListItems = prefijoListItems;
    }

    public List<SelectItem> getTipoViaListItems() {
        return tipoViaListItems;
    }

    public void setTipoViaListItems(List<SelectItem> tipoViaListItems) {
        this.tipoViaListItems = tipoViaListItems;
    }

    public List<DatoComplementario> getDatoComplementarioList() {
        return datoComplementarioList;
    }

    public void setDatoComplementarioList(List<DatoComplementario> datoComplementarioList) {
        this.datoComplementarioList = datoComplementarioList;
    }

    public List<Prefijo> getPrefijoList() {
        return prefijoList;
    }

    public void setPrefijoList(List<Prefijo> prefijoList) {
        this.prefijoList = prefijoList;
    }

    public List<TipoVia> getTipoViaList() {
        return tipoViaList;
    }

    public void setTipoViaList(List<TipoVia> tipoViaList) {
        this.tipoViaList = tipoViaList;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Inicializar los controladores">
    public void initControllers() {
        FacesContext context = FacesContext.getCurrentInstance();
        datoComplementarioJpaController = (DatoComplementarioJpaController) context.getELContext().getELResolver().getValue(context.getELContext(), null, "datoComplementarioJpaController");
        prefijoJpaController = (PrefijoJpaController) context.getELContext().getELResolver().getValue(context.getELContext(), null, "prefijoJpaController");
        tipoViaJpaController = (TipoViaJpaController) context.getELContext().getELResolver().getValue(context.getELContext(), null, "tipoViaJpaController");
        
    }

    public boolean areControllersInitilized(Object[] cont) {
        for (int i = 0; i < cont.length; i++) {
            if (cont[i] == null) {
                return false;
            }
        }
        return true;
    }
    //</editor-fold>
}
